package lab03.test12;

import java.sql.*;

/**
 * Created by dell on 2017/12/28.
 */
public class Test12 {

    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            try {
                connection = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/java_2017?serverTimezone=UTC",
                        "root",
                        "123456"
                );
            } catch (SQLException e) {
                e.printStackTrace();
            }

            statement = connection.createStatement();

            System.out.println("遍历:");
            resultSet = statement.executeQuery("SELECT * FROM t_users");
            while(resultSet.next()){
                System.out.print(resultSet.getInt(1)+" \t");
                System.out.print(resultSet.getString(2)+" \t");
                System.out.print(resultSet.getString(3)+" \t");
                System.out.print(resultSet.getLong(4)+" \t");
                System.out.println();
            }

            System.out.println("查询用户名为'admin'的记录:");
            resultSet = statement.executeQuery("select * from t_users WHERE username = 'admin'");
            while(resultSet.next()){
                System.out.print(resultSet.getInt(1)+" \t");
                System.out.print(resultSet.getString(2)+" \t");
                System.out.print(resultSet.getString(3)+" \t");
                System.out.print(resultSet.getLong(4)+" \t");
                System.out.println();
            }

            System.out.println("查询用户名为'admin'，密码为'123456'的记录:");
            resultSet = statement.executeQuery("select * from t_users WHERE username = " +
                    "'admin' AND password = '123456'");
            while(resultSet.next()){
                System.out.print(resultSet.getInt(1)+" \t");
                System.out.print(resultSet.getString(2)+" \t");
                System.out.print(resultSet.getString(3)+" \t");
                System.out.print(resultSet.getLong(4)+" \t");
                System.out.println();
            }

            System.out.println("查询注册时间大于10000000的记录:");
            resultSet = statement.executeQuery("select * from t_users WHERE register_datetime >" +
                    " 10000000");
            while(resultSet.next()){
                System.out.print(resultSet.getInt(1)+" \t");
                System.out.print(resultSet.getString(2)+" \t");
                System.out.print(resultSet.getString(3)+" \t");
                System.out.print(resultSet.getLong(4)+" \t");
                System.out.println();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
